package org.example.springmvcdemo.controller;

import org.example.springmvcdemo.model.UserInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class UserController {
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String fun() {
        return "hello";
    }

    @RequestMapping("/r1")
    public String r1(String name) {
        return "接收到的 name: " + name;
    }

    @RequestMapping("/r2")
    public String r2(Integer age) {
        return "age = " + age;
    }
    @RequestMapping("/r3")
    public String r3(String name, Integer age) {
        return "name = " + name + ", age = " + age;
    }

    @RequestMapping("/r4")
    public String r4(UserInfo user) {
        return user.toString();
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam("name") String username, Integer age) {
        return "username = " + username + ", age = " + age;
    }

    @RequestMapping("/r6")
    public String r6(int[] arr) {
        return Arrays.toString(arr) + ", length = " + arr.length;
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "list", required = false) List<String> list) {
        if (list != null) {
            return list.toString() + ", size = " + list.size();
        }
        return "list为空!";
    }

    @RequestMapping("/r8")
    public String r8(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/r9/{articleId}")
    public String r9(@PathVariable Integer articleId) {
        return "articleId = " + articleId;
    }

    @RequestMapping("/r10/{name}/{age}")
    public String r10(@PathVariable String name,@PathVariable Integer age) {
        return "name = " + name + ", age = " + age;
    }

    @RequestMapping("/r11")
    public String r11(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/画图/" + fileName));
        return "获取上传的文件: " + fileName;
    }

}
